package com.db.collections;

import java.util.*;

/**
 * @author Alexander Kotov (kotov.alex.22@gmail.com)
 */
public class SpeedTest {

    final static int ELEMENTS_COUNT = 1000000;
    final static int ITERATIONS = 1000;

    private void run() {

        IntHashMap intHashMap = new IntHashMap();
        IntHashMap2 intHashMap2 = new IntHashMap2();
        HashMap<Integer, Integer> hashMap = new HashMap<Integer, Integer>();

        Integer[] randomKeyArray = new Integer[ELEMENTS_COUNT];
        Integer[] randomValueArray = new Integer[ELEMENTS_COUNT];
        Integer[] randomKeyToGetArray = new Integer[ITERATIONS];

        for (int i = 0; i != ELEMENTS_COUNT; ++i) {
            randomKeyArray[i] = (int)(Math.random() * (Integer.MAX_VALUE));
            randomValueArray[i] = (int)(Math.random() * (Integer.MAX_VALUE));
            intHashMap.put(randomKeyArray[i], randomValueArray[i]);
            intHashMap2.put(randomKeyArray[i], randomValueArray[i]);
            hashMap.put(randomKeyArray[i], randomValueArray[i]);
        }

        for (int i = 0; i != ITERATIONS; ++i) {
            randomKeyToGetArray[i] = randomKeyArray[(int)(Math.random() * (randomKeyArray.length))];
        }

        //Get IntHashMap
        long start = System.nanoTime();

        long res = 0;
        for (int i = 0; i != ITERATIONS; ++i) {
            res += intHashMap.get(randomKeyToGetArray[i]);
        }

        long time = (System.nanoTime() - start) / ITERATIONS;

        System.out.println(res);
        System.out.println("IntHashMap get: " + time + " nanosec");
        System.out.println("================================");

        //Get IntHashMap2
        start = System.nanoTime();

        for (int i = 0; i != ITERATIONS; ++i) {
            res += intHashMap2.get(randomKeyToGetArray[i]);
        }

        time = (System.nanoTime() - start) / ITERATIONS;

        System.out.println(res);
        System.out.println("IntHashMap2 get: " + time + " nanosec");
        System.out.println("================================");


        //Get HashMap
        start = System.nanoTime();

        for (int i = 0; i != ITERATIONS; ++i) {
            res += hashMap.get(randomKeyToGetArray[i]);
        }

        time = (System.nanoTime() - start) / ITERATIONS;

        System.out.println(res);
        System.out.println("HashMap get: " + time + " nanosec");
        System.out.println("================================");


        hashMap.clear();
        intHashMap.clear();
        intHashMap2.clear();

        //Put IntHashMap
        start = System.nanoTime();

        for (int i = 0; i != ELEMENTS_COUNT; ++i) {
            intHashMap.put(randomKeyArray[i], randomValueArray[i]);
        }

        time = (System.nanoTime() - start) / ELEMENTS_COUNT;

        System.out.println(intHashMap.hashCode());
        System.out.println("IntHashMap put: " + time + " nanosec");
        System.out.println("================================");

        //Put IntHashMap2
        start = System.nanoTime();

        for (int i = 0; i != ELEMENTS_COUNT; ++i) {
            intHashMap2.put(randomKeyArray[i], randomValueArray[i]);
        }

        time = (System.nanoTime() - start) / ELEMENTS_COUNT;

        System.out.println(intHashMap2.hashCode());
        System.out.println("IntHashMap2 put: " + time + " nanosec");
        System.out.println("================================");


        //Put HashMap
        start = System.nanoTime();

        for (int i = 0; i != ELEMENTS_COUNT; ++i) {
            hashMap.put(randomKeyArray[i], randomValueArray[i]);
        }

        time = (System.nanoTime() - start) / ELEMENTS_COUNT;

        System.out.println(hashMap.hashCode());
        System.out.println("HashMap put: " + time + " nanosec");
        System.out.println("================================");

    }

    public static void main(String[] args) {
        SpeedTest speedTest = new SpeedTest();
        speedTest.run();
    }
}
